﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace ShredTheLog.Api.DataContracts.Languages
{
    [XmlRoot("LangPack")]
    public class LanguagePack
    {
        [XmlAttribute("Group")]
        public string Name { get; set; }

        [XmlElement("R")]
        public LanguagePackRow[] Items 
        {
            get { return _items; }
            set 
            { 
                _items = value;
                _rowDictionary = null;
                if (Items != null)
                {
                    _rowDictionary = Items.ToDictionary(row => row.Id, row => row.Text);
                }
            } 
        }

        private LanguagePackRow[] _items;

        private Dictionary<int, string> _rowDictionary;

        public override string ToString()
        {
            if (String.IsNullOrEmpty(Name))
                return base.ToString();
            else
                return Name;
        }

        public string this[int id]
        {
            get
            {
                if (_rowDictionary == null)
                    throw new InvalidOperationException();

                string result;
                if (!_rowDictionary.TryGetValue(id, out result))
                    result = "-";
                return result;
            }
        }
    }
}
